﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Serialization;
using System.Xml.Linq;
using SGP;

namespace Tile_Editor
{
    public partial class Form1 : Form
    {
        Size TileSetSize = new Size(64, 64);
        Size tileSize = new Size(64, 64);
        Map map = new Map();
        Point selectedTile = new Point(0, 0);
        Point Test = new Point(0, 0);
        BindingList<String> TriggerList = new BindingList<string>();
        bool graphic;
        public bool Graphic
        {
            get { return graphic; }
            set { graphic = value; }
        }
        bool logic;
        public bool Logic
        {
            get { return logic; }
            set { logic = value; }
        }
        private bool trigger;
        public bool Trigger
        {
            get { return trigger; }
            set { trigger = value; }
        }
        private bool mouse;
        public bool Mouse
        {
            get { return mouse; }
            set { mouse = value; }
        }
        private bool draw;
        public bool Draw
        {
            get { return draw; }
            set { draw = value; }
        }
        private bool fill;
        public bool Fill
        {
            get { return fill; }
            set { fill = value; }
        }
        private bool select;
        public bool Select1
        {
            get { return select; }
            set { select = value; }
        }
        public bool looping;
        public bool Looping
        {
            get { return looping; }
            set { looping = value; }
        }
        private bool none;
        public bool None
        {
            get { return none; }
            set { none = value; }
        }
        private bool flag;
        public bool Flag
        {
            get { return flag; }
            set { flag = value; }
        }
        private bool playerarea;
        public bool Playerarea
        {
            get { return playerarea; }
            set { playerarea = value; }
        }
        private bool collision;
        public bool Collision
        {
            get { return collision; }
            set { collision = value; }
        }
        private bool addunit;
        public bool Addunit
        {
            get { return addunit; }
            set { addunit = value; }
        }
        private bool triggerevent;
        public bool Triggerevent
        {
            get { return triggerevent; }
            set { triggerevent = value; }
        }
        private bool addheight;
        public bool Addheight
        {
            get { return addheight; }
            set { addheight = value; }
        }
        private bool decreasemove;
        public bool Decreasemove
        {
            get { return decreasemove; }
            set { decreasemove = value; }
        }
        private bool defensebonus;
        public bool Defensebonus
        {
            get { return defensebonus; }
            set { defensebonus = value; }
        }
        private bool newevent;
        public bool Newevent
        {
            get { return newevent; }
            set { newevent = value; }
        }
        private bool node;
        public bool Node
        {
            get { return node; }
            set { node = value; }
        }
        private bool someevent;
        public bool Someevent
        {
            get { return someevent; }
            set { someevent = value; }
        }
        private bool general;
        public bool General
        {
            get { return general; }
            set { general = value; }
        }
        private bool melee;
        public bool Melee
        {
            get { return melee; }
            set { melee = value; }
        }
        private bool ranger;
        public bool Ranger
        {
            get { return ranger; }
            set { ranger = value; }
        }
        private bool priest;
        public bool Priest
        {
            get { return priest; }
            set { priest = value; }
        }
        private bool tank;
        public bool Tank
        {
            get { return tank; }
            set { tank = value; }
        }
        private string holder;
        public string Holder
        {
            get { return holder; }
            set { holder = value; }
        }
        CSGP_Direct3D D3d = CSGP_Direct3D.GetInstance();
        CSGP_TextureManager TM = CSGP_TextureManager.GetInstance();
        int m_nTileID = -1;
        string filename;

        public string Filename
        {
            get { return filename; }
            set { filename = value; }
        }
        public Form1()
        {
            InitializeComponent();
            Size mapDocSize = Size.Empty;
            mapDocSize.Width = map.MapSize.Width * tileSize.Width;
            mapDocSize.Height = map.MapSize.Height * tileSize.Height;
            Panel.Panel1.AutoScrollMinSize = mapDocSize;
            Graphics g = Panel.Panel1.CreateGraphics();
            g.Dispose();
            TriggerList.Add("None");
            TriggerList.Add("Flag");
            TriggerList.Add("Player Start");
            TriggerList.Add("Collision");
            TriggerList.Add("Add Unit");
            TriggerList.Add("Trigger Event");
            TriggerList.Add("Add Power Node");
            TriggerList.Add("Add Height");
            TriggerList.Add("Add Move Decrease");
            TriggerList.Add("Add Defense Bonus");
            TriggerList.Add("Add New Event");
            STrigger.DataSource = TriggerList;
            unit.DataSource = Enum.GetNames(typeof(Unit));
        }
        public void Init()
        {
            D3d.Initialize(splitContainer1.Panel1, false);
            D3d.AddRenderTarget(Panel.Panel1);
            TM.Initialize(D3d.Device, D3d.Sprite);
            looping = true;
            numericUpDown1.Value = map.MapSize.Height;
            numericUpDown2.Value = map.MapSize.Width;
            numericUpDown3.Value = tileSize.Height;
            numericUpDown4.Value = tileSize.Width;
            String filepath = System.IO.Path.GetFullPath(System.IO.Directory.GetCurrentDirectory() + "\\..\\..\\..\\Tacwars\\resource\\Images\\tilesetpok.png");
            Filename = System.IO.Path.GetFileName(filepath);
            m_nTileID = TM.LoadTexture(filepath);
            TileSetSize.Height = TM.GetTextureHeight(m_nTileID);
            TileSetSize.Width = TM.GetTextureWidth(m_nTileID);
            Size tileSetDocSize = Size.Empty;
            tileSetDocSize.Width = TileSetSize.Width;
            tileSetDocSize.Height = TileSetSize.Height;
            splitContainer1.Panel1.AutoScrollMinSize = tileSetDocSize;
            Graphics g = splitContainer1.Panel1.CreateGraphics();
            g.Dispose();
            Trigger = Fill = Select1 = false;
            Draw = true;
            Graphic = true;
            Logic = true;
            None = true;
            General = true;
            mouse = false;
        }
        public new void Update()
        {

        }
        public void Render()
        {
            RenderMap();
            RenderTile();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            looping = false;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
        public void RenderMap()
        {
            D3d.Clear(Panel.Panel1, Color.FromArgb(255, 255, 255, 255));
            D3d.DeviceBegin();
            D3d.SpriteBegin();
            Point offset = new Point(0, 0);
            offset.X += Panel.Panel1.AutoScrollPosition.X;
            offset.Y += Panel.Panel1.AutoScrollPosition.Y;

            for (int y = 0; y < map.MapSize.Height; y++)
            {
                for (int x = 0; x < map.MapSize.Width; x++)
                {
                    Rectangle destRect = Rectangle.Empty;
                    destRect.X = x * tileSize.Width + offset.X;
                    destRect.Y = y * tileSize.Height + offset.Y;
                    destRect.Size = tileSize;
                    D3d.DrawHollowRect(destRect, Color.Black, 1);
                    Rectangle srcRect = Rectangle.Empty;
                    srcRect.X = map.Tiles[x, y].tile.X * tileSize.Width;
                    srcRect.Y = map.Tiles[x, y].tile.Y * tileSize.Height;
                    if (map.Tiles[x, y].selectpaint == false)
                        srcRect.Size = tileSize;
                    else
                    {
                        srcRect.Width = Test.X * tileSize.Width;
                        srcRect.Height = Test.Y * tileSize.Height;
                    }
                    if (x == map.Tiles[x, y].position.X && y == map.Tiles[x, y].position.Y && Graphic == true && map.Tiles[x, y].painted == true)
                        TM.Draw(map.Tiles[x,y].m_nTileID, destRect.X, destRect.Y, 1.0f, 1.0f, srcRect);
                    //if (x == map.Tiles[x, y].position.X && y == map.Tiles[x, y].position.Y && Graphic == true && map.Tiles[x, y].painted == true)
                    //{
                    //    Rectangle srcRect2 = Rectangle.Empty;
                    //    srcRect2.X = map.Tiles[x, y].tile.X * tileSize.Width;
                    //    srcRect2.Y = map.Tiles[x, y].tile.Y * tileSize.Height;
                    //    srcRect2.Width = Test.X * tileSize.Width;
                    //    srcRect2.Height = Test.Y * tileSize.Height;
                    //    TM.Draw(m_nTileID, destRect.X, destRect.Y, 1.0f, 1.0f, srcRect2);

                    //}
                    if (Logic == true)
                    {
                        if (map.Tiles[x, y].logic == true)
                        {
                            D3d.DrawRect(destRect, Color.FromArgb(100, Color.Blue));
                            if (tileSize.Width == 64 && tileSize.Height == 64)
                            {
                                if (map.Tiles[x, y].flag == true)
                                    D3d.DrawText("F", destRect.X, destRect.Y, Color.Black);
                                if (map.Tiles[x, y].highground == 1)
                                    D3d.DrawText("H", destRect.X + 20, destRect.Y, Color.Black);
                                if (map.Tiles[x, y].moveamount >= 1)
                                    D3d.DrawText("M", destRect.X + 40, destRect.Y, Color.Black);
                                if (map.Tiles[x, y].Startarea == true)
                                    D3d.DrawText("S", destRect.X, destRect.Y + 20, Color.Black);
                                if (map.Tiles[x, y].defenseamount >= 1)
                                    D3d.DrawText("D", destRect.X + 20, destRect.Y + 20, Color.Black);
                                if (map.Tiles[x, y].TriggerEvent != null || map.Tiles[x, y].ExtraEvent != null)
                                    D3d.DrawText("E", destRect.X + 40, destRect.Y + 20, Color.Black);
                                if (map.Tiles[x, y].node == true)
                                    D3d.DrawText("PN", destRect.X, destRect.Y + 40, Color.Black);
                                if (map.Tiles[x, y].collision == true)
                                    D3d.DrawText("C", destRect.X + 40, destRect.Y + 40, Color.Black);
                            }
                        }
                    }
                }
            }
            D3d.SpriteEnd();
            D3d.DeviceEnd();
            D3d.Present();
        }
        public void RenderTile()
        {
            D3d.Clear(splitContainer1.Panel1, Color.FromArgb(255, 255, 255, 255));
            D3d.DeviceBegin();
            D3d.SpriteBegin();
            Point offset = new Point(0, 0);
            offset.X += splitContainer1.Panel1.AutoScrollPosition.X;
            offset.Y += splitContainer1.Panel1.AutoScrollPosition.Y;
            int test = TM.GetTextureWidth(m_nTileID) / tileSize.Width;
            if (m_nTileID != -1)
                TM.Draw(m_nTileID, offset.X, offset.Y);
            for (int x = 0; x < TM.GetTextureWidth(m_nTileID) / tileSize.Width; x++)
            {
                for (int y = 0; y < TM.GetTextureHeight(m_nTileID) / tileSize.Width; y++)
                {
                    Rectangle destRect = Rectangle.Empty;
                    destRect.X = x * tileSize.Width + offset.X;
                    destRect.Y = y * tileSize.Height + offset.Y;
                    destRect.Size = tileSize;
                    D3d.DrawHollowRect(destRect, Color.Black, 1);
                    if (Select1 == false)
                    {
                        Rectangle selectRect = Rectangle.Empty;
                        selectRect.X = selectedTile.X * tileSize.Width + offset.X;
                        selectRect.Y = selectedTile.Y * tileSize.Height + offset.Y;
                        selectRect.Size = tileSize;
                        D3d.DrawHollowRect(selectRect, Color.Red, 2);
                    }
                    else
                    {
                        Rectangle selectRect = Rectangle.Empty;
                        selectRect.X = selectedTile.X * tileSize.Width + offset.X;
                        selectRect.Y = selectedTile.Y * tileSize.Height + offset.Y;
                        selectRect.Width = Test.X * tileSize.Width + offset.X;
                        selectRect.Height = Test.Y * tileSize.Height + offset.Y;
                        D3d.DrawHollowRect(selectRect, Color.Red, 2);

                    }

                }
            }


            D3d.SpriteEnd();
            D3d.DeviceEnd();
            D3d.Present();
        }
        private void importTileSetToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog opdlg = new OpenFileDialog();
            opdlg.InitialDirectory = System.IO.Path.GetFullPath(System.IO.Directory.GetCurrentDirectory() + "\\..\\..\\..\\Tacwars\\resource\\Images");

            if (DialogResult.OK == opdlg.ShowDialog())
            {
                Filename = System.IO.Path.GetFileName(opdlg.FileName);
                m_nTileID = TM.LoadTexture(opdlg.FileName);
                TileSetSize.Height = TM.GetTextureHeight(m_nTileID);
                TileSetSize.Width = TM.GetTextureWidth(m_nTileID);
                Size tileSetDocSize = Size.Empty;
                tileSetDocSize.Width = TileSetSize.Width;
                tileSetDocSize.Height = TileSetSize.Height;
                splitContainer1.Panel1.AutoScrollMinSize = tileSetDocSize;
                Graphics g = splitContainer1.Panel1.CreateGraphics();
                g.Dispose();
            }
        }

        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
            map.mapSize.Height = (int)numericUpDown1.Value;
            Size mapDocSize = Size.Empty;
            mapDocSize.Width = map.MapSize.Width * tileSize.Width;
            mapDocSize.Height = map.MapSize.Height * tileSize.Height;
            Panel.Panel1.AutoScrollMinSize = mapDocSize;
            Graphics g = Panel.Panel1.CreateGraphics();
            g.Dispose();
        }

        private void numericUpDown2_ValueChanged(object sender, EventArgs e)
        {
            map.mapSize.Width = (int)numericUpDown2.Value;
            Size mapDocSize = Size.Empty;
            mapDocSize.Width = map.MapSize.Width * tileSize.Width;
            mapDocSize.Height = map.MapSize.Height * tileSize.Height;
            Panel.Panel1.AutoScrollMinSize = mapDocSize;
            Graphics g = Panel.Panel1.CreateGraphics();
            g.Dispose();
        }

        private void Panel_Panel2_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (Draw == true)
                {
                    Point offset = Point.Empty;
                    offset.X -= splitContainer1.Panel1.AutoScrollPosition.X;
                    offset.Y -= splitContainer1.Panel1.AutoScrollPosition.Y;
                    selectedTile.X = (e.X + offset.X) / tileSize.Width;
                    selectedTile.Y = (e.Y + offset.Y) / tileSize.Height;
                }
                else if (Trigger == true)
                {

                }
                else if (Fill == true)
                {
                    Point offset = Point.Empty;
                    offset.X -= splitContainer1.Panel1.AutoScrollPosition.X;
                    offset.Y -= splitContainer1.Panel1.AutoScrollPosition.Y;
                    selectedTile.X = (e.X + offset.X) / tileSize.Width;
                    selectedTile.Y = (e.Y + offset.Y) / tileSize.Height;
                }
                else
                {

                }
            }
        }

        private void numericUpDown3_ValueChanged(object sender, EventArgs e)
        {
            tileSize.Height = (int)numericUpDown3.Value;
            Size mapDocSize = Size.Empty;
            mapDocSize.Width = map.MapSize.Width * tileSize.Width;
            mapDocSize.Height = map.MapSize.Height * tileSize.Height;
            Panel.Panel1.AutoScrollMinSize = mapDocSize;
            Graphics g = Panel.Panel1.CreateGraphics();
            g.Dispose();
        }

        private void numericUpDown4_ValueChanged(object sender, EventArgs e)
        {
            tileSize.Width = (int)numericUpDown4.Value;
            Size mapDocSize = Size.Empty;
            mapDocSize.Width = map.MapSize.Width * tileSize.Width;
            mapDocSize.Height = map.MapSize.Height * tileSize.Height;
            Panel.Panel1.AutoScrollMinSize = mapDocSize;
            Graphics g = Panel.Panel1.CreateGraphics();
            g.Dispose();
        }

        private void Panel_Panel1_MouseMove(object sender, MouseEventArgs e)
        {
            Point offset = Point.Empty;
            offset.X -= Panel.Panel1.AutoScrollPosition.X;
            offset.Y -= Panel.Panel1.AutoScrollPosition.Y;
            //selectedTile.X = (e.X + offset.X) / tileSize.Width;
            // selectedTile.Y = (e.Y + offset.Y) / tileSize.Height;
            toolStripTextBox1.Text = "Mouse Location [X= " + ((e.Location.X + offset.X) / tileSize.Width + 1) + " Y= " + ((e.Location.Y + offset.Y) / tileSize.Height + 1) + "]";
            if (e.Button == MouseButtons.Left)
            {
                if (Draw == true)
                {
                    int x = (e.X + offset.X) / tileSize.Width;
                    int y = (e.Y + offset.Y) / tileSize.Height;
                    if (x < 0)
                        x = 0;
                    if (y < 0)
                        y = 0;
                    if (x > 24)
                        x = 24;
                    if (y > 24)
                        y = 24;
                    map.Battlemap[x, y] = selectedTile;
                    map.Tiles[x, y].tile = selectedTile;
                    map.Tiles[x, y].position.X = x;
                    map.Tiles[x, y].position.Y = y;
                    map.Tiles[x, y].painted = true;
                    map.Tiles[x, y].FileType = Filename;
                    map.Tiles[x, y].m_nTileID = m_nTileID;


                }
                else if (Trigger == true)
                {
                    if (None == true)
                    {
                    }
                    else if (Flag == true)
                    {
                        int x = (e.X + offset.X) / tileSize.Width;
                        int y = (e.Y + offset.Y) / tileSize.Height;
                        if (x < 0)
                            x = 0;
                        if (y < 0)
                            y = 0;
                        if (x > 24)
                            x = 24;
                        if (y > 24)
                            y = 24;
                        map.Tiles[x, y].position.X = x;
                        map.Tiles[x, y].position.Y = y;
                        map.Tiles[x, y].flag = true;
                        map.Tiles[x, y].logic = true;
                    }
                    else if (Playerarea == true)
                    {
                        int x = (e.X + offset.X) / tileSize.Width;
                        int y = (e.Y + offset.Y) / tileSize.Height;
                        if (x < 0)
                            x = 0;
                        if (y < 0)
                            y = 0;
                        if (x > 24)
                            x = 24;
                        if (y > 24)
                            y = 24;
                        map.Tiles[x, y].position.X = x;
                        map.Tiles[x, y].position.Y = y;
                        map.Tiles[x, y].Startarea = true;
                        map.Tiles[x, y].logic = true;

                    }
                    else if (Collision == true)
                    {
                        int x = (e.X + offset.X) / tileSize.Width;
                        int y = (e.Y + offset.Y) / tileSize.Height;
                        if (x < 0)
                            x = 0;
                        if (y < 0)
                            y = 0;
                        if (x > 24)
                            x = 24;
                        if (y > 24)
                            y = 24;
                        map.Tiles[x, y].position.X = x;
                        map.Tiles[x, y].position.Y = y;
                        map.Tiles[x, y].logic = true;
                        map.Tiles[x, y].collision = true;

                    }
                    else if (Addunit == true)
                    {
                        int x = (e.X + offset.X) / tileSize.Width;
                        int y = (e.Y + offset.Y) / tileSize.Height;
                        if (x < 0)
                            x = 0;
                        if (y < 0)
                            y = 0;
                        if (x > 24)
                            x = 24;
                        if (y > 24)
                            y = 24;
                        map.Tiles[x, y].position.X = x;
                        map.Tiles[x, y].position.Y = y;
                        map.Tiles[x, y].logic = true;
                        if (General == true)
                        {
                            map.Tiles[x, y].type = Unit.General;
                        }
                        else if (Melee == true)
                        {
                            map.Tiles[x, y].type = Unit.Beserker;
                        }
                        else if (Tank == true)
                        {
                            map.Tiles[x, y].type = Unit.Knight;
                        }
                        else if (Priest == true)
                        {
                            map.Tiles[x, y].type = Unit.Priest;
                        }
                        else if (Ranger == true)
                        {
                            map.Tiles[x, y].type = Unit.Ranger;
                        }
                    }
                    else if (Triggerevent == true)
                    {
                        int x = (e.X + offset.X) / tileSize.Width;
                        int y = (e.Y + offset.Y) / tileSize.Height;
                        if (x < 0)
                            x = 0;
                        if (y < 0)
                            y = 0;
                        if (x > 24)
                            x = 24;
                        if (y > 24)
                            y = 24;
                        map.Tiles[x, y].position.X = x;
                        map.Tiles[x, y].position.Y = y;
                        map.Tiles[x, y].logic = true;
                        map.Tiles[x, y].TriggerEvent = Holder;
                    }
                    else if (Addheight == true)
                    {
                        int x = (e.X + offset.X) / tileSize.Width;
                        int y = (e.Y + offset.Y) / tileSize.Height;
                        if (x < 0)
                            x = 0;
                        if (y < 0)
                            y = 0;
                        if (x > 24)
                            x = 24;
                        if (y > 24)
                            y = 24;
                        map.Tiles[x, y].position.X = x;
                        map.Tiles[x, y].position.Y = y;
                        map.Tiles[x, y].logic = true;
                        map.Tiles[x, y].highground = 1;
                    }
                    else if (Decreasemove == true)
                    {
                        int x = (e.X + offset.X) / tileSize.Width;
                        int y = (e.Y + offset.Y) / tileSize.Height;
                        if (x < 0)
                            x = 0;
                        if (y < 0)
                            y = 0;
                        if (x > 24)
                            x = 24;
                        if (y > 24)
                            y = 24;
                        map.Tiles[x, y].position.X = x;
                        map.Tiles[x, y].position.Y = y;
                        map.Tiles[x, y].logic = true;
                        map.Tiles[x, y].moveamount = (int)movedefchange.Value;
                    }
                    else if (Defensebonus == true)
                    {
                        int x = (e.X + offset.X) / tileSize.Width;
                        int y = (e.Y + offset.Y) / tileSize.Height;
                        if (x < 0)
                            x = 0;
                        if (y < 0)
                            y = 0;
                        if (x > 24)
                            x = 24;
                        if (y > 24)
                            y = 24;
                        map.Tiles[x, y].position.X = x;
                        map.Tiles[x, y].position.Y = y;
                        map.Tiles[x, y].logic = true;
                        map.Tiles[x, y].defenseamount = (int)movedefchange.Value;
                    }
                    else if (Someevent == true)
                    {
                        int x = (e.X + offset.X) / tileSize.Width;
                        int y = (e.Y + offset.Y) / tileSize.Height;
                        if (x < 0)
                            x = 0;
                        if (y < 0)
                            y = 0;
                        if (x > 24)
                            x = 24;
                        if (y > 24)
                            y = 24;
                        map.Tiles[x, y].position.X = x;
                        map.Tiles[x, y].position.Y = y;
                        map.Tiles[x, y].logic = true;
                        map.Tiles[x, y].ExtraEvent = STrigger.SelectedIndex.ToString();
                    }
                    else if (Node == true)
                    {
                        int x = (e.X + offset.X) / tileSize.Width;
                        int y = (e.Y + offset.Y) / tileSize.Height;
                        if (x < 0)
                            x = 0;
                        if (y < 0)
                            y = 0;
                        if (x > 24)
                            x = 24;
                        if (y > 24)
                            y = 24;
                        map.Tiles[x, y].position.X = x;
                        map.Tiles[x, y].position.Y = y;
                        map.Tiles[x, y].logic = true;
                        map.Tiles[x, y].node = true;
                    }
                }
                else if (Fill == true)
                {

                }
                else
                {
                    int x = (e.X + offset.X) / tileSize.Width;
                    int y = (e.Y + offset.Y) / tileSize.Height;
                    if (x < 0)
                        x = 0;
                    if (y < 0)
                        y = 0;
                    if (x > 24)
                        x = 24;
                    if (y > 24)
                        y = 24;
                    map.Battlemap[x, y] = selectedTile;
                    map.Tiles[x, y].tile = selectedTile;
                    map.Tiles[x, y].position.X = x;
                    map.Tiles[x, y].position.Y = y;
                    map.Tiles[x, y].painted = true;
                    map.Tiles[x, y].selectrecttile = Test;
                    map.Tiles[x, y].selectpaint = true;
                    map.Tiles[x, y].FileType = Filename;
                    map.Tiles[x, y].m_nTileID = m_nTileID;

                }
            }
        }



        private void event_btn_Click(object sender, EventArgs e)
        {
            Trigger = true;
            Draw = false;
            Fill = false;
            Select1 = false;
        }

        private void Draw_btn_Click(object sender, EventArgs e)
        {
            Draw = true;
            Trigger = false;
            Fill = false;
            Select1 = false;
        }

        private void Fill_btn_Click(object sender, EventArgs e)
        {
            Fill = true;
            Draw = false;
            Trigger = false;
            Select1 = false;
        }

        private void SelectRect_btn_Click(object sender, EventArgs e)
        {
            Select1 = true;
            Draw = false;
            Fill = false;
            Trigger = false;
        }

        private void graphics_btn_Click(object sender, EventArgs e)
        {
            Graphic = !Graphic;
        }

        private void logic_btn_Click(object sender, EventArgs e)
        {
            Logic = !Logic;
        }

        private void Panel_Panel1_MouseDown(object sender, MouseEventArgs e)
        {
            Point offset = Point.Empty;
            offset.X -= Panel.Panel1.AutoScrollPosition.X;
            offset.Y -= Panel.Panel1.AutoScrollPosition.Y;
            if (Draw == true)
            {
                int x = (e.X + offset.X) / tileSize.Width;
                int y = (e.Y + offset.Y) / tileSize.Height;
                map.Battlemap[x, y] = selectedTile;
                map.Tiles[x, y].tile = selectedTile;
                map.Tiles[x, y].position.X = x;
                map.Tiles[x, y].position.Y = y;
                map.Tiles[x, y].painted = true;
                map.Tiles[x, y].FileType = Filename;
                map.Tiles[x, y].m_nTileID = m_nTileID;
            }
            else if (Trigger == true)
            {
                if (None == true)
                {
                }
                else if (Flag == true)
                {
                    int x = (e.X + offset.X) / tileSize.Width;
                    int y = (e.Y + offset.Y) / tileSize.Height;
                    map.Tiles[x, y].position.X = x;
                    map.Tiles[x, y].position.Y = y;
                    map.Tiles[x, y].logic = true;
                    map.Tiles[x, y].flag = true;
                }
                else if (Playerarea == true)
                {
                    int x = (e.X + offset.X) / tileSize.Width;
                    int y = (e.Y + offset.Y) / tileSize.Height;
                    map.Tiles[x, y].position.X = x;
                    map.Tiles[x, y].position.Y = y;
                    map.Tiles[x, y].logic = true;
                    map.Tiles[x, y].Startarea = true;
                }
                else if (Collision == true)
                {
                    int x = (e.X + offset.X) / tileSize.Width;
                    int y = (e.Y + offset.Y) / tileSize.Height;
                    map.Tiles[x, y].position.X = x;
                    map.Tiles[x, y].position.Y = y;
                    map.Tiles[x, y].logic = true;
                    map.Tiles[x, y].collision = true;

                }
                else if (Addunit == true)
                {
                    int x = (e.X + offset.X) / tileSize.Width;
                    int y = (e.Y + offset.Y) / tileSize.Height;
                    map.Tiles[x, y].position.X = x;
                    map.Tiles[x, y].position.Y = y;
                    map.Tiles[x, y].logic = true;
                    if (General == true)
                    {
                        map.Tiles[x, y].type = Unit.General;
                    }
                    else if (Melee == true)
                    {
                        map.Tiles[x, y].type = Unit.Beserker;
                    }
                    else if (Tank == true)
                    {
                        map.Tiles[x, y].type = Unit.Knight;
                    }
                    else if (Priest == true)
                    {
                        map.Tiles[x, y].type = Unit.Priest;
                    }
                    else if (Ranger == true)
                    {
                        map.Tiles[x, y].type = Unit.Ranger;
                    }
                }
                else if (Triggerevent == true)
                {
                    int x = (e.X + offset.X) / tileSize.Width;
                    int y = (e.Y + offset.Y) / tileSize.Height;
                    map.Tiles[x, y].position.X = x;
                    map.Tiles[x, y].position.Y = y;
                    map.Tiles[x, y].logic = true;
                    map.Tiles[x, y].TriggerEvent = Holder;
                }
                else if (Addheight == true)
                {
                    int x = (e.X + offset.X) / tileSize.Width;
                    int y = (e.Y + offset.Y) / tileSize.Height;
                    map.Tiles[x, y].position.X = x;
                    map.Tiles[x, y].position.Y = y;
                    map.Tiles[x, y].logic = true;
                    map.Tiles[x, y].highground = 1;
                }
                else if (Decreasemove == true)
                {
                    int x = (e.X + offset.X) / tileSize.Width;
                    int y = (e.Y + offset.Y) / tileSize.Height;
                    map.Tiles[x, y].position.X = x;
                    map.Tiles[x, y].position.Y = y;
                    map.Tiles[x, y].logic = true;
                    map.Tiles[x, y].moveamount = (int)movedefchange.Value;
                }
                else if (Defensebonus == true)
                {
                    int x = (e.X + offset.X) / tileSize.Width;
                    int y = (e.Y + offset.Y) / tileSize.Height;
                    map.Tiles[x, y].position.X = x;
                    map.Tiles[x, y].position.Y = y;
                    map.Tiles[x, y].logic = true;
                    map.Tiles[x, y].defenseamount = (int)movedefchange.Value;
                }
                else if (Someevent == true)
                {
                    int x = (e.X + offset.X) / tileSize.Width;
                    int y = (e.Y + offset.Y) / tileSize.Height;
                    map.Tiles[x, y].position.X = x;
                    map.Tiles[x, y].position.Y = y;
                    map.Tiles[x, y].logic = true;
                    map.Tiles[x, y].ExtraEvent = STrigger.Items[STrigger.SelectedIndex].ToString();
                }
                else if (Node == true)
                {
                    int x = (e.X + offset.X) / tileSize.Width;
                    int y = (e.Y + offset.Y) / tileSize.Height;
                    map.Tiles[x, y].position.X = x;
                    map.Tiles[x, y].position.Y = y;
                    map.Tiles[x, y].node = true;
                }
            }
            else if (Fill == true)
            {

            }

            else if (Mouse == true)
            {
                int x = (e.X + offset.X) / tileSize.Width;
                int y = (e.Y + offset.Y) / tileSize.Height;
                label3.Text = "Tile Info at " + map.Tiles[x, y].position;
                Tileinfobox.Text = "Flag:" + map.Tiles[x, y].flag.ToString()
                    + "\nPlayer Start Area:" + map.Tiles[x, y].Startarea.ToString()
                    + "\nCollision:" + map.Tiles[x, y].collision.ToString()
                    + "\nEnemy Unit:" + map.Tiles[x, y].type.ToString()
                    + "\nTrigger Event:" + map.Tiles[x, y].TriggerEvent
                    + "\nPower Node:" + map.Tiles[x, y].node.ToString()
                    + "\nHigh Ground:" + (map.Tiles[x, y].highground.ToString() == "1" ? "High" : "Low")
                    + "\nMove Decrease:" + map.Tiles[x, y].moveamount.ToString()
                    + "\nDefense Bonus:" + map.Tiles[x, y].defenseamount.ToString()
                    + "\n" + map.Tiles[x, y].ExtraEvent;

            }
            else
            {
                int x = (e.X + offset.X) / tileSize.Width;
                int y = (e.Y + offset.Y) / tileSize.Height;
                map.Battlemap[x, y] = selectedTile;
                map.Tiles[x, y].tile = selectedTile;
                map.Tiles[x, y].position.X = x;
                map.Tiles[x, y].position.Y = y;
                map.Tiles[x, y].painted = true;
                map.Tiles[x, y].selectrecttile = Test;
                map.Tiles[x, y].selectpaint = true;
                map.Tiles[x, y].FileType = Filename;
                map.Tiles[x, y].m_nTileID = m_nTileID;

            }
        }

        private void Panel_Panel2_MouseMove(object sender, MouseEventArgs e)
        {


            if (e.Button == MouseButtons.Left)
            {
                if (Draw == true)
                {
                    Point offset = Point.Empty;
                    offset.X -= splitContainer1.Panel1.AutoScrollPosition.X;
                    offset.Y -= splitContainer1.Panel1.AutoScrollPosition.Y;
                    selectedTile.X = (e.X + offset.X) / tileSize.Width;
                    selectedTile.Y = (e.Y + offset.Y) / tileSize.Height;

                }
                else if (Trigger == true)
                {
                    Point offset = Point.Empty;
                    offset.X -= splitContainer1.Panel1.AutoScrollPosition.X;
                    offset.Y -= splitContainer1.Panel1.AutoScrollPosition.Y;
                    selectedTile.X = (e.X + offset.X) / tileSize.Width;
                    selectedTile.Y = (e.Y + offset.Y) / tileSize.Height;
                }
                else if (Fill == true)
                {
                    Point offset = Point.Empty;
                    offset.X -= splitContainer1.Panel1.AutoScrollPosition.X;
                    offset.Y -= splitContainer1.Panel1.AutoScrollPosition.Y;
                    selectedTile.X = (e.X + offset.X) / tileSize.Width;
                    selectedTile.Y = (e.Y + offset.Y) / tileSize.Height;
                }
                else
                {
                    //Point Test = selectedTile;
                    //Point Test2 = new Point() ;
                    //Point Test3;
                    Point offset = Point.Empty;
                    offset.X -= splitContainer1.Panel1.AutoScrollPosition.X;
                    offset.Y -= splitContainer1.Panel1.AutoScrollPosition.Y;
                    //selectedTile.X = (e.X + offset.X) / tileSize.Width;
                    // selectedTile.Y = (e.Y + offset.Y) / tileSize.Height;
                    Test.X = ((e.X + offset.X)) / tileSize.Width;// tileSize.Width);
                    Test.Y = ((e.Y + offset.Y)) / tileSize.Height; /// tileSize.Height);
                    //Test2.X = ((e.X + offset.X) / tileSize.Width);
                    // Test2.Y = ((e.Y + offset.Y) / tileSize.Height);

                }
            }
        }

        private void STrigger_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (STrigger.SelectedValue.ToString() == "None")
            {
                None = true;
                unit.Visible = false;
                movedefchange.Visible = false;
                Flag = false;
                Playerarea = false;
                Collision = false;
                Addunit = false;
                Triggerevent = false;
                Addheight = false;
                Decreasemove = false;
                Defensebonus = false;
                Newevent = false;
                Someevent = false;
                Node = false;

            }
            else if (STrigger.SelectedValue.ToString() == "Flag")
            {
                Flag = true;
                unit.Visible = false;
                movedefchange.Visible = false;
                None = false;
                Playerarea = false;
                Collision = false;
                Addunit = false;
                Triggerevent = false;
                Addheight = false;
                Decreasemove = false;
                Defensebonus = false;
                Newevent = false;
                Someevent = false;
                Node = false;

            }
            else if (STrigger.SelectedValue.ToString() == "Player Start")
            {
                Playerarea = true;
                unit.Visible = false;
                movedefchange.Visible = false;
                Flag = false;
                None = false;
                Collision = false;
                Addunit = false;
                Triggerevent = false;
                Addheight = false;
                Decreasemove = false;
                Defensebonus = false;
                Newevent = false;
                Someevent = false;
                Node = false;

            }
            else if (STrigger.SelectedValue.ToString() == "Collision")
            {
                Collision = true;
                unit.Visible = false;
                movedefchange.Visible = false;
                Flag = false;
                Playerarea = false;
                None = false;
                Addunit = false;
                Triggerevent = false;
                Addheight = false;
                Decreasemove = false;
                Defensebonus = false;
                Newevent = false;
                Someevent = false;
                Node = false;

            }
            else if (STrigger.SelectedValue.ToString() == "Add Unit")
            {
                Addunit = true;
                unit.Visible = true;
                movedefchange.Visible = false;
                Flag = false;
                Playerarea = false;
                Collision = false;
                None = false;
                Triggerevent = false;
                Addheight = false;
                Decreasemove = false;
                Defensebonus = false;
                Newevent = false;
                Someevent = false;
                Node = false;

            }
            else if (STrigger.SelectedValue.ToString() == "Trigger Event")
            {
                Triggerevent = true;
                unit.Visible = false;
                movedefchange.Visible = false;
                Flag = false;
                Playerarea = false;
                Collision = false;
                Addunit = false;
                None = false;
                Addheight = false;
                Decreasemove = false;
                Defensebonus = false;
                Newevent = false;
                Someevent = false;
                Node = false;
                Event dlg = new Event();
                if (DialogResult.OK == dlg.ShowDialog())
                {
                    this.Holder = dlg.TriggerEvent;
                }

            }
            else if (STrigger.SelectedValue.ToString() == "Add Height")
            {
                Addheight = true;
                unit.Visible = false;
                movedefchange.Visible = false;
                Flag = false;
                Playerarea = false;
                Collision = false;
                Addunit = false;
                Triggerevent = false;
                None = false;
                Decreasemove = false;
                Defensebonus = false;
                Newevent = false;
                Someevent = false;
                Node = false;


            }
            else if (STrigger.SelectedValue.ToString() == "Add Move Decrease")
            {
                Decreasemove = true;
                unit.Visible = false;
                movedefchange.Visible = true;
                Flag = false;
                Playerarea = false;
                Collision = false;
                Addunit = false;
                Triggerevent = false;
                Addheight = false;
                None = false;
                Defensebonus = false;
                Newevent = false;
                Someevent = false;
                Node = false;

            }
            else if (STrigger.SelectedValue.ToString() == "Add Defense Bonus")
            {
                Defensebonus = true;
                unit.Visible = false;
                movedefchange.Visible = true;
                Flag = false;
                Playerarea = false;
                Collision = false;
                Addunit = false;
                Triggerevent = false;
                Addheight = false;
                Decreasemove = false;
                None = false;
                Newevent = false;
                Someevent = false;
                Node = false;

            }
            else if (STrigger.SelectedValue.ToString() == "Add New Event")
            {
                Newevent = true;
                unit.Visible = false;
                movedefchange.Visible = false;
                Flag = false;
                Playerarea = false;
                Collision = false;
                Addunit = false;
                Triggerevent = false;
                Addheight = false;
                Decreasemove = false;
                Defensebonus = false;
                None = false;
                Someevent = false;
                Node = false;
                Event dlg = new Event();
                dlg.Name = "New Event";
                if (DialogResult.OK == dlg.ShowDialog())
                {
                    this.Holder = dlg.TriggerEvent;
                    TriggerList.Add(Holder);
                }
                //STrigger.DataSource = TriggerList;
                // STrigger.Items.Add(TriggerList.Last());
            }
            else if (STrigger.SelectedValue.ToString() == "Add Power Node")
            {
                Node = true;
                unit.Visible = false;
                movedefchange.Visible = false;
                Flag = false;
                Playerarea = false;
                Collision = false;
                Addunit = false;
                Triggerevent = false;
                Addheight = false;
                Decreasemove = false;
                Defensebonus = false;
                Newevent = false;
                Someevent = false;
            }
            else
            {
                Someevent = true;
                unit.Visible = false;
                movedefchange.Visible = false;
                Flag = false;
                Playerarea = false;
                Collision = false;
                Addunit = false;
                Triggerevent = false;
                Addheight = false;
                Decreasemove = false;
                Defensebonus = false;
                Newevent = false;
                None = false;
                Node = false;
            }
        }
        private void unit_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (unit.SelectedValue.ToString() == "General")
            {
                General = true;
                Tank = false;
                Melee = false;
                Ranger = false;
                Priest = false;
            }
            else if (unit.SelectedValue.ToString() == "Knight")
            {
                Tank = true;
                General = false;
                Melee = false;
                Ranger = false;
                Priest = false;
            }
            else if (unit.SelectedValue.ToString() == "Beserker")
            {
                Melee = true;
                Tank = false;
                General = false;
                Ranger = false;
                Priest = false;
            }
            else if (unit.SelectedValue.ToString() == "Ranger")
            {
                Ranger = true;
                Tank = false;
                Melee = false;
                General = false;
                Priest = false;
            }
            else if (unit.SelectedValue.ToString() == "Priest")
            {
                Priest = true;
                Tank = false;
                Melee = false;
                Ranger = false;
                General = false;
            }
        }

        private void Mouse_btn_Click(object sender, EventArgs e)
        {
            Mouse = !Mouse;
            Draw = false;
            Trigger = false;
            Select1 = false;
            Fill = false;

        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog opdlg = new SaveFileDialog();
            opdlg.InitialDirectory = System.IO.Path.GetFullPath(System.IO.Directory.GetCurrentDirectory() + "\\..\\..\\..\\Tacwars\\resource\\Maps");
            opdlg.Filter = "ALL Files|*.*|Xml Files| *.xml";
            opdlg.FilterIndex = 2;
            opdlg.DefaultExt = "xml";
            if (DialogResult.OK == opdlg.ShowDialog())
            {
                XElement xRoot = new XElement("Map");
                XElement xMap = new XElement("Level");
                xRoot.Add(xMap);
                XAttribute xMapHeight = new XAttribute("SizeY", map.mapSize.Height);
                xMap.Add(xMapHeight);
                XAttribute xMapWidth = new XAttribute("SizeX", map.mapSize.Width);
                xMap.Add(xMapWidth);
                for (int y = 0; y < map.mapSize.Height; y++)
                {
                    for (int x = 0; x < map.mapSize.Width; x++)
                    {
                        XElement xTile = new XElement("Tile");
                        xMap.Add(xTile);
                        XAttribute xNode = new XAttribute("Node", (map.Tiles[x, y].node == true ? 1 : 0));
                        xTile.Add(xNode);
                        XAttribute XHigh = new XAttribute("HighGround", map.Tiles[x, y].highground);
                        xTile.Add(XHigh);
                        XAttribute xFlag = new XAttribute("Flag", (map.Tiles[x, y].flag == true ? 1 : 0));
                        xTile.Add(xFlag);
                        XAttribute xPosX = new XAttribute("PosX", map.Tiles[x, y].position.X);
                        xTile.Add(xPosX);
                        XAttribute xPosY = new XAttribute("PosY", map.Tiles[x, y].position.Y);
                        xTile.Add(xPosY);
                        XAttribute xMove = new XAttribute("Move", map.Tiles[x, y].moveamount);
                        xTile.Add(xMove);
                        XAttribute xDefense = new XAttribute("Defense", map.Tiles[x, y].defenseamount);
                        xTile.Add(xDefense);
                        XAttribute xCollision = new XAttribute("Collision", (map.Tiles[x, y].collision == true ? 1 : 0));
                        xTile.Add(xCollision);
                        XAttribute xsrcX = new XAttribute("TileRectX", map.Tiles[x, y].tile.X);
                        xTile.Add(xsrcX);
                        XAttribute xsrcY = new XAttribute("TileRectY", map.Tiles[x, y].tile.Y);
                        xTile.Add(xsrcY);
                        if (map.Tiles[x, y].selectpaint == true)
                        {
                            XAttribute xsrcwidth = new XAttribute("TileWidth", map.Tiles[x, y].selectrecttile.X);
                            xTile.Add(xsrcwidth);
                            XAttribute xsrcheight = new XAttribute("TileHeight", map.Tiles[x, y].selectrecttile.Y);
                            xTile.Add(xsrcheight);
                            XAttribute xselectpaint = new XAttribute("SelectPaint", (map.Tiles[x, y].selectpaint == true ? 1 : 0));
                            xTile.Add(xselectpaint);
                        }
                        else
                        {
                            XAttribute xsrcwidth = new XAttribute("TileWidth", tileSize.Width);
                            xTile.Add(xsrcwidth);
                            XAttribute xsrcheight = new XAttribute("TileHeight", tileSize.Height);
                            xTile.Add(xsrcheight);
                        }
                        XAttribute xpaint = new XAttribute("Paint", (map.Tiles[x, y].painted == true ? 1 : 0));
                        xTile.Add(xpaint);
                        XAttribute xStart = new XAttribute("Start", (map.Tiles[x, y].Startarea == true ? 1 : 0));
                        xTile.Add(xStart);
                        if (map.Tiles[x, y].logic == true)
                        {
                            XElement xUnit = new XElement("Unit", map.Tiles[x, y].type);
                            xTile.Add(xUnit);
                        }
                        XElement xEvent = new XElement("Event", map.Tiles[x, y].ExtraEvent);
                        xTile.Add(xEvent);
                        XElement xFile = new XElement("File", map.Tiles[x, y].FileType);
                        xTile.Add(xFile);
                    }
                }
                xRoot.Save(opdlg.FileName);

            }
        }

        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            map = new Map();
            TileSetSize = new Size(64, 64);
            tileSize = new Size(64, 64);
            map = new Map();
            selectedTile = new Point(0, 0);
            Test = new Point(0, 0);
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "ALL Files | *.* | Xml Files| *.xml";
            dlg.FilterIndex = 2;
            dlg.InitialDirectory = System.IO.Path.GetFullPath(System.IO.Directory.GetCurrentDirectory() + "\\..\\..\\..\\Tacwars\\resource\\Maps");
            if (DialogResult.OK == dlg.ShowDialog())
            {
                map = new Map();
                XElement XRoot = XElement.Load(dlg.FileName);
                XElement xLevel = XRoot.Element("Level");
                XAttribute xmapwidth = xLevel.Attribute("SizeY");
                map.mapSize.Width = Convert.ToInt32(xmapwidth.Value);
                XAttribute xmapHeight = xLevel.Attribute("SizeX");
                map.mapSize.Height = Convert.ToInt32(xmapHeight.Value);
                Size mapDocSize = Size.Empty;
                mapDocSize.Width = map.MapSize.Width * tileSize.Width;
                mapDocSize.Height = map.MapSize.Height * tileSize.Height;
                Panel.Panel1.AutoScrollMinSize = mapDocSize;
                Graphics g = Panel.Panel1.CreateGraphics();
                g.Dispose();
                IEnumerable<XElement> xTiles = xLevel.Elements();
                foreach (XElement XTile in xTiles)
                {
                    XAttribute xNode = XTile.Attribute("Node");
                    int node = Convert.ToInt32(xNode.Value);
                    XAttribute XHigh = XTile.Attribute("HighGround");
                    int High = Convert.ToInt32(XHigh.Value);
                    XAttribute xFlag = XTile.Attribute("Flag");
                    int Flag = Convert.ToInt32(xFlag.Value);
                    XAttribute xPosX = XTile.Attribute("PosX");
                    int x = Convert.ToInt32(xPosX.Value);
                    XAttribute xPosY = XTile.Attribute("PosY");
                    int y = Convert.ToInt32(xPosY.Value);
                    map.Tiles[x, y].position.X = x;
                    map.Tiles[x, y].position.Y = y;
                    XAttribute xMove = XTile.Attribute("Move");
                    int move = Convert.ToInt32(xMove.Value);
                    XAttribute xDefense = XTile.Attribute("Defense");
                    int defense = Convert.ToInt32(xDefense.Value);
                    XAttribute xCollision = XTile.Attribute("Collision");
                    int collision = Convert.ToInt32(xCollision.Value);
                    XAttribute xsrcX = XTile.Attribute("TileRectX");
                    int srcx = Convert.ToInt32(xsrcX.Value);
                    XAttribute xsrcY = XTile.Attribute("TileRectY");
                    int srcy = Convert.ToInt32(xsrcY.Value);
                    XAttribute xPaint = XTile.Attribute("Paint");
                    int paint = Convert.ToInt32(xPaint.Value);
                    XAttribute xselectpaint = XTile.Attribute("SelectPaint");
                    int selectpaint = Convert.ToInt32(xselectpaint);
                    if (selectpaint == 1)
                    {
                        XAttribute xselectsrcwidth = XTile.Attribute("TileWidth");
                        int selectwidth = Convert.ToInt32(xselectsrcwidth);
                        XAttribute xselectsrcheight = XTile.Attribute("TileHeight");
                        int selectheight = Convert.ToInt32(xselectsrcheight);
                        map.Tiles[x, y].selectrecttile.X = selectwidth;
                        map.Tiles[x, y].selectrecttile.Y = selectheight;
                        map.Tiles[x, y].selectpaint = (selectpaint == 1 ? true : false);
                    }
                    XAttribute xsrcwidth = XTile.Attribute("TileWidth");
                    int width = Convert.ToInt32(xsrcwidth.Value);
                    XAttribute xsrcheight = XTile.Attribute("TileHeight");
                    int height = Convert.ToInt32(xsrcheight.Value);
                    XAttribute xStart = XTile.Attribute("Start");
                    int start = Convert.ToInt32(xStart.Value);
                    XElement xUnit = XTile.Element("Unit");
                    //string unit = Convert.ToString(xUnit);
                    XElement xEvent = XTile.Element("Event");
                    //string
                    XElement xFile = XTile.Element("File");
                    string file = Convert.ToString(xFile.Value);
                    map.Tiles[x, y].node = (node == 1 ? true : false);
                    if (node == 1)
                        map.Tiles[x, y].logic = true;
                    map.Tiles[x, y].highground = High;
                    if (High == 1)
                        map.Tiles[x, y].logic = true;
                    map.Tiles[x, y].flag = (Flag == 1 ? true : false);
                    if (Flag == 1)
                        map.Tiles[x, y].logic = true;
                    map.Tiles[x, y].moveamount = move;
                    if (move > 0)
                        map.Tiles[x, y].logic = true;
                    map.Tiles[x, y].defenseamount = defense;
                    if (defense > 0)
                        map.Tiles[x, y].logic = true;
                    map.Tiles[x, y].collision = (collision == 1 ? true : false);
                    if (collision == 1)
                        map.Tiles[x, y].logic = true;
                    map.Tiles[x, y].tile.X = srcx;
                    map.Tiles[x, y].tile.Y = srcy;
                    map.Tiles[x, y].Startarea = (start == 1 ? true : false);
                    if (start == 1)
                        map.Tiles[x, y].logic = true;
                    if (selectpaint == 0)
                    {
                        tileSize.Width = width;
                        tileSize.Height = height;
                    }
                    //map.Tiles[x, y].type = unit;
                    m_nTileID = TM.LoadTexture(file);
                    map.Tiles[x, y].m_nTileID = m_nTileID;
                    map.Tiles[x, y].painted = (paint == 1 ? true : false);
                }
            }
            for (int i = 0; i < map.MapSize.Width; i++)
            {
                for (int j = 0; j < map.MapSize.Height; j++)
                {
                    if (map.Tiles[i, j].m_nTileID >= 0)
                    {
                        m_nTileID = map.Tiles[i, j].m_nTileID;
                        break;
                    }
                }
            }
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void splitContainer1_Panel1_MouseDown(object sender, MouseEventArgs e)
        {
            if (Select1)
            {
                Point offset = Point.Empty;
                offset.X -= splitContainer1.Panel1.AutoScrollPosition.X;
                offset.Y -= splitContainer1.Panel1.AutoScrollPosition.Y;
                selectedTile.X = (e.X + offset.X) / tileSize.Width;
                selectedTile.Y = (e.Y + offset.Y) / tileSize.Height;
            }
        }
    }
}

